home *** CD-ROM | disk | FTP | other *** search
- Path: frco.com!usenet
- From: Jadam@tcmail.frco.com (Jim Adam)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Missinterpreting *protected* access modifier?
- Date: 30 Jan 1996 19:10:06 GMT
- Organization: Fisher Rosemount Systems
- Message-ID: <4elqee$gqp@rolaids.frco.com>
- References: <DLrCGF.G1F@wn.planet.gen.nz>
- NNTP-Posting-Host: primrose.frco.com
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.11
-
- In article <DLrCGF.G1F@wn.planet.gen.nz>, hugp@kp.planet.gen.nz says...
-
- >Am I missinterpreting the meaning of the *protected* access modifier?
- >The following code does not compile under VC++:
-
- > ============================================
- > class A
- > {
- > protected:
- > int i;
- > int GetInt() { return i; };
- > };
- >
- > class B : public A
- > {
- > protected:
- > int Foo(A * pA) { return pA->GetInt(); } // ERROR
- > };
- > ============================================
-
- Yes, you get a compiler error when you call "pA->GetInt()." The
- "protected" access means that you can legally do:
-
- int B::Bar()
- {
- GetInt(); // CALL THE INHERITED GetInt()
- }
-
- You can also do:
-
- int B::Foo( B & b )
- {
- b.GetInt(); // ACCESS PROTECTED STUFF OF ANOTHER INSTANCE
- }
-
- But you can't do:
-
- class C : public A { ... };
-
- int B::Foo( C & c )
- {
- C.GetInt(); // ERROR: SIBLING ACCESS NOT ALLOWED
- }
-
- Even though C and B both inherit from A, B can't access
- protected members in C. It can't even access the "common"
- protected members inherited from A, such as GetInt().
-
- Jim
-
-